<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
class RollimgController extends Controller
{
     public function __construct()
     {
         $this -> middleware('login');
     }
     public function add()
     {
        $data = DB::table('home_pic') -> get();
        return view('admin.rollimg.add', ['title' => '添加前台轮播图','data' => $data]);
     }
     public function insert(Request $request)
     {
         if($request -> hasFile('pic')){
             if($request -> file('pic') -> isValid())
             {
                //处理头像
                //获取文件后缀
                $extension = $request -> file('pic') -> getClientOriginalExtension();

                $filename = mt_rand(100000,999999999).'.'.$extension;

                $dir = './uploads/homepic/';

                $request -> file('pic') -> move($dir, $filename);

                $data['pic'] = '/uploads/homepic/'.$filename;
                $time = date('Y:m:d H:i:s');
                $data['created_at'] = $time;
                $data['updated_at'] = $time;
                $res = DB::table('home_pic') -> insertGetId($data);
                if($res)
                 {
                     return redirect('/admin/rollimg/index') -> with(['info' => '更新成功']);
                 }else
                 {
                     return back() -> with(['info' => '更新失败']);
                 }

             }
         }
    }

    public function index()
    {
         $data = DB::table('home_pic') -> get();
         return view('admin.rollimg.index',['title' => '轮播图浏览','data' => $data]);
    }
    public function edit($id)
    {
         $data = DB::table('home_pic') -> where('id',$id) -> first();
         return view('admin.rollimg.edit',['title' => '轮播图修改','data' => $data]);
    }
    public function update(Request $request)
    {
          $id = $request -> id;
          if($request -> hasFile('pic'))
          {
              if($request -> file('pic') -> isValid())
              {
                    $extension = $request -> file('pic') -> getClientOriginalExtension();

                    $filename = mt_rand(100000,999999999).'.'.$extension;

                    $dir = './uploads/homepic/';

                    $request -> file('pic') -> move($dir, $filename);

                    $data['pic'] = '/uploads/homepic/'.$filename;

                    $time = date('Y:m:d H:i:s');

                    $data['updated_at'] = $time;

                    $oldPic = DB::table('home_pic') -> where('id',$id) -> first() -> pic;

                    if(!empty($oldPic))
                    {
                      if(file_exists('.'.$oldPic))
                      {
                          unlink('.'.$oldPic);
                      }
                    }

              }
          }
         $res = DB::table('home_pic') -> where('id',$id) -> update($data);
         if($res)
         {
             return redirect('admin/rollimg/index') -> with(['info' => '更新成功']);
         }else
         {
             return back() -> with(['info' => '更新失败']);
         }
    }

    public function delete($id)
    {
          $oldPic = DB::table('home_pic') -> where('id',$id) -> first() -> pic;
          $res = DB::table('home_pic') -> where('id',$id) -> delete();
          if($res)
          {
              if(!empty($oldPic))
              {
                   if(file_exists('.'.$oldPic))
                   {
                       unlink('.'.$oldPic);
                   }
              }

              return redirect('admin/rollimg/index') -> with(['info' => '删除成功']);
          }else
          {
              return back() -> with(['info' => '删除失败']);
          }
    }
}
